package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(timeout = 1000,retries = 2)
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public void importOrderSettings(List<String[]> list) {
        List<OrderSetting> orderSettingList = stringToOrderSetting(list);
        for (OrderSetting orderSetting : orderSettingList) {
            QueryWrapper<OrderSetting> wrapper = new QueryWrapper<>();
            wrapper.eq("ORDERDATE",orderSetting.getOrderDate());
            OrderSetting orderSettingIsExist = baseMapper.selectOne(wrapper);
            if (orderSettingIsExist!=null){
                orderSetting.setId(orderSettingIsExist.getId());
            }
        }
        this.saveOrUpdateBatch(orderSettingList);
    }

    private List<OrderSetting> stringToOrderSetting(List<String[]> list) {
        if (list!=null&&list.size()!=0) {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSettingList.add(orderSetting);
            }
            return orderSettingList;
        }
        return null;
    }

    @Override
    public Map findSettingData(int year, int month) {
        String beginDate = year+"-"+month+"-01";
        String endDate = year+"-"+month+"-31";
        QueryWrapper<OrderSetting> wrapper = new QueryWrapper<>();
        wrapper.between("ORDERDATE",beginDate,endDate);
        List<OrderSetting> orderSettingList = baseMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            String date2String = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            Map<String, Object> hashMap = new HashMap<>();
            hashMap.put("number",orderSetting.getNumber());
            hashMap.put("reservations",orderSetting.getReservations());
            map.put(date2String,hashMap);
        }
        return map;
    }

    @Override
    public Boolean updateNumberByOrderDate(int number,String orderdate) {
        QueryWrapper<OrderSetting> wrapper = new QueryWrapper<>();
        wrapper.eq("ORDERDATE",orderdate);
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setNumber(number);
        return this.update(orderSetting,wrapper);
    }

    @Override
    public int isOrderOkByOrderDate(String orderDate) {
        return 0;
    }

    @Override
    public void updateReservationsByOrderDate(String orderDate) {

    }
}
